package com.example.mylistview;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * 功能：
 * 作者：HeXiangGuang
 * 日期：2024年05月15日
 */
public class MyAdapter extends BaseAdapter {
    private List<Bean> data;
    private Context context;
    public MyAdapter(List<Bean> data, Context context) {
        this.data = data;
        this.context = context;
    }

    //getCount显示list中数据有多少条显示多少条
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

/*    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
        }
        //下面 convertView.findViewById(R.id.tv)这里每次调用比较消耗性能
        TextView textView = convertView.findViewById(R.id.tv);
        textView.setText(data.get(position).getName());
        Log.e("leo","getview: " + position);
        return convertView;
    }*/

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null){
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
            viewHolder.textView = convertView.findViewById(R.id.tv);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.textView.setText(data.get(position).getName());
        Log.e("leo","getview: " + position);
        return convertView;
    }
    private final class ViewHolder{
        TextView textView;
    }
}
